//
//  CQAppMainTabBarController.m
//  TSBaseVCDemo
//
//  Created by ciyouzen on 2018/8/29.
//  Copyright © 2018年 dvlproad. All rights reserved.
//
#import "CQAppMainTabBarController.h"

@interface CQAppMainTabBarController ()


@end

@implementation CQAppMainTabBarController


#pragma mark - Overwrite
// 修复[Swift——preferredStatusBarStyle不执行](https://www.jianshu.com/p/395acc02234b)
- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.selectedViewController;
}


#pragma mark - LifeCycle
- (instancetype)initWithVCs:(NSArray<UIViewController *> *)viewControllers
             vcJsonFileURLs:(NSArray<NSURL *> *)vcJsonFileURLs
{
    NSMutableArray *tabBarItemsAttributes = [[NSMutableArray alloc] init];
    for (NSURL *vcJsonFileURL in vcJsonFileURLs) {
        NSDictionary *tabBarItemsAttribute = [self __tabBarItemsAttributesWithVCJsonFileURL:vcJsonFileURL];
        [tabBarItemsAttributes addObject:tabBarItemsAttribute];
    }
    
    UIEdgeInsets imageInsets = UIEdgeInsetsZero;//UIEdgeInsetsMake(4.5, 0, -4.5, 0);
    UIOffset titlePositionAdjustment = UIOffsetMake(0, 0);
    self = [super initWithViewControllers:viewControllers
                    tabBarItemsAttributes:tabBarItemsAttributes
                              imageInsets:imageInsets
                  titlePositionAdjustment:titlePositionAdjustment
                                  context:nil
            ];
    if (self) {
        [self customizeTabBarAppearanceWithTitlePositionAdjustment:titlePositionAdjustment];
    }
    return self;
}



- (NSDictionary *)__tabBarItemsAttributesWithVCJsonFileURL:(NSURL *)vcJsonFileURL {
    // 特别注意：要正确显示lottie图标，必须确保跟视图控制器的标题是使用self.navigationItem.title设置，而不能是使用self.title
    NSDictionary *tabBarItemsAttributes = @{
                                            //CYLTabBarItemImageInsets:[NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)],
                                            CYLTabBarLottieURL      :vcJsonFileURL,
                                            CYLTabBarLottieSize: [NSValue valueWithCGSize:CGSizeMake(34, 34)]
                                            };
    return tabBarItemsAttributes;
}


/**
 *  更多TabBar自定义设置：比如：tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性等等
 */
- (void)customizeTabBarAppearanceWithTitlePositionAdjustment:(UIOffset)titlePositionAdjustment {
    // Customize UITabBar height
    // 自定义 TabBar 高度
    //self.tabBarHeight = 60;
    
    // 设置 TabBar 背景
    // 半透明
//    [UITabBar appearance].translucent = YES;
//    [UITabBar appearance].barTintColor = [UIColor redColor];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    

    // NO.1，using Image note:recommended.推荐方式
    // set the bar shadow image
    // without shadow : use -[[CYLTabBarController hideTabBarShadowImageView] in CYLMainRootViewController.m
    if (@available(iOS 13.0, *)) {
        UITabBarItemAppearance *inlineLayoutAppearance = [[UITabBarItemAppearance  alloc] init];
        // fix https://github.com/ChenYilong/CYLTabBarController/issues/456
        inlineLayoutAppearance.normal.titlePositionAdjustment = titlePositionAdjustment;

        // set the text Attributes
        UITabBarAppearance *standardAppearance = [[UITabBarAppearance alloc] init];
        standardAppearance.stackedLayoutAppearance = inlineLayoutAppearance;
        standardAppearance.backgroundColor = [UIColor whiteColor];
        //shadowColor和shadowImage均可以自定义颜色, shadowColor默认高度为1, shadowImage可以自定义高度.
         standardAppearance.shadowImage = [[self class] imageWithColor:[UIColor clearColor] size:CGSizeMake([UIScreen mainScreen].bounds.size.width, 1)];
        self.tabBar.standardAppearance = standardAppearance;
    } else {
        // Override point for customization after application launch.
        // set the text Attributes
        // 设置文字属性
        //UITabBarItem *tabBar = [UITabBarItem appearance];
        // This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.
        [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
        [[UITabBar appearance] setShadowImage:[[self class] imageWithColor:[UIColor clearColor] size:CGSizeMake([UIScreen mainScreen].bounds.size.width, 1)]];
    }
}


//- (void)viewDidLoad {
//    [super viewDidLoad];
//
//    [self customizeInterface];
//}
//
//- (void)customizeInterface {
//    //添加小红点
//    //添加提示动画，引导用户点击
//    [self setViewDidLayoutSubViewsBlockInvokeOnce:YES block:^(CYLTabBarController *tabBarController) {
//        NSUInteger delaySeconds = 1.5;
//        dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delaySeconds * NSEC_PER_SEC));
//        dispatch_after(when, dispatch_get_main_queue(), ^{
//            @try {
//                UIViewController *viewController0 = tabBarController.viewControllers[0];
//                [viewController0 cyl_setBadgeBackgroundColor:[UIColor redColor]];
//                [viewController0 cyl_showBadgeValue:@"2" animationType:CYLBadgeAnimationTypeNone];
//                
//            } @catch (NSException *exception) {}
//        });
//    }];
//}


#pragma mark - Private Method
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width + 1, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


@end
